package com.example.contentprovider;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;

import java.util.List;

public class MyContentProvider extends ContentProvider {
    public static final Uri QUERY_ALL = Uri.parse(
            "content://com.imti.contentprovider/stu/job/queryall");

    public static final Uri INSERT_DATA = Uri.parse(
            "content://com.imti.contentprovider/stu/job/insert");
    private Student student;


    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        // Implement this to handle requests to delete one or more rows.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public String getType(Uri uri) {
        // TODO: Implement this to handle requests for the MIME type of the data
        // at the given URI.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public Uri insert(Uri uri, ContentValues values) {
        return student.insertData(parseTableName(uri),values);
    }

    /**
     * ContentProvider初始化方法
     * @return  true则表示ContentProvider加载成功,false则加载失败
     */
    @Override
    public boolean onCreate() {
        // TODO: Implement this to initialize your content provider on startup.
        //此处创建SQLite数据库对象，即new MySQLiteOpenHelper对象
        student =new Student(getContext(),"stu.db",null,1);
        return true;
    }
    public String parseTableName(Uri uri){
        //通过Uri解析出表名及参数
        //content://com.imti.contentprovider/stu/job/queryall
        List<String> pathlist = uri.getPathSegments();
        String dbName = pathlist.get(0);
        String tableName = pathlist.get(1);

        switch (dbName){
            //判断是否为学生数据库
            case "stu":
                //判断是否为学生就业信息表
                if (tableName.equals("job")){
                    return "usergrade";
                }
                break;
        }
        return "";
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection,
                        String[] selectionArgs, String sortOrder) {
        return student.queryAll(parseTableName(uri));
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection,
                      String[] selectionArgs) {
        // TODO: Implement this to handle requests to update one or more rows.
        throw new UnsupportedOperationException("Not yet implemented");
    }
}